package com.ftg.learn.controller;

import com.ftg.learn.dto.SalePart;
import com.ftg.learn.entity.SaleEntity;
import com.ftg.learn.sercice.ISaleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 控制器类
 *
 * @author wangty
 * @date 2022/10/18
 */

@RestController
@Api(tags = "商品模块")
@RequestMapping("/sale")
public class SaleController {

    @Resource
    ISaleService iSaleService;

    /**
     * 这是用来全部商品信息的方法
     *
     * @return List<SaleEntity>
     */
    @ApiOperation(value = "全部商品列表")
    @GetMapping("/showAll")
    public List<SaleEntity> showAll() {
        return iSaleService.showAll();
    }

    /**
     * 模糊查询一个参数
     *
     * @param name 商品名称
     * @return
     */
    @ApiOperation(value = "模糊查询一个参数")
    @GetMapping("/likeWithName")
    public List<SaleEntity> likeWithName(
            @RequestParam(value = "name") @ApiParam(value = "商品名称", example = "康师傅水", required = true) String name
    ) {
        return iSaleService.likeWithName(name);
    }

    /**
     * 模拟动态查询，可填可不填的两个参数
     *
     * @param name    商品名称
     * @param address 商品地址
     * @return
     */
    @ApiOperation(value = "动态查询")
    @GetMapping("/likeWithNameAddress")
    public List<SaleEntity> likeWithNameAddress(
            @RequestParam(value = "name", required = false) @ApiParam(value = "商品名称", example = "康师傅水") String name,
            @RequestParam(value = "address", required = false, defaultValue = "") @ApiParam(value = "地址", example = "松北") String address
    ) {
        return iSaleService.likeWithNameAddress(name, address);
    }

    /**
     * 分页查询
     *
     * @param page    页数
     * @param limit   一页显示几条
     * @param name    商品名称
     * @param address 商品地址
     * @return
     */
    @ApiOperation(value = "带查询的分页")
    @GetMapping("/pageWithDynamic")
    public Map<String, Object> pageWithNameAddress(
            @RequestParam(value = "page") @ApiParam(value = "当前页数", example = "1", defaultValue = "1", required = true) Integer page
            , @RequestParam(value = "limit") @ApiParam(value = "显示条数", example = "10", defaultValue = "10", required = true) Integer limit
            , @RequestParam(value = "name", required = false) @ApiParam(value = "商品名称", example = "康师傅水") String name
            , @RequestParam(value = "address", required = false) @ApiParam(value = "地址", example = "松北") String address
    ) {
        return iSaleService.pageWithNameAddress(page, limit, name, address);
    }


    /**
     * 这是用来添加商品的方法
     *
     * @param saleEntity 商品实例
     * @return 添加商品信息
     */
    @ApiOperation(value = "按照实例SaleEnity添加")
    @PostMapping("/addEntity")
    public int addEntity(
            @ApiParam(value = "addEntity", example = "{id:1,name:'商品'...}") @RequestBody SaleEntity saleEntity) {
        return iSaleService.addEntity(saleEntity);
    }

    /**
     * 按照dto进行添加商品
     *
     * @param salePart
     * @return
     */
    @ApiOperation(value = "按照dto添加")
    @PostMapping("/addDto")
    public int addDto(SalePart salePart) {
        return iSaleService.addDto(salePart);
    }

    /**
     * 按照实例修改
     *
     * @param saleEntity 实例
     * @return
     */
    @ApiOperation(value = "实体修改")
    @PutMapping("/update")
    public int updateSaleEntity(
            @ApiParam(value = "saleEntity", example = "{id:1,name:'商品'...}") @RequestBody SaleEntity saleEntity
    ) {
        return iSaleService.updateSaleEntity(saleEntity);
    }

    /**
     * 按照主键进行修改的功能
     *
     * @param saleEntity 实例
     * @param id         编号
     * @return
     */
    @ApiOperation(value = "带主键修改功能")
    @PutMapping("/updateWithId")
    public int updateSaleEntityWithId(
            @ApiParam(value = "saleEntity", example = "{id:1,name:'商品',address:'地址'...}") @RequestBody SaleEntity saleEntity,
            @RequestParam(value = "id") @ApiParam(value = "主键", example = "12", required = true) Integer id
    ) {
        return iSaleService.updateSaleEntityWithId(saleEntity, id);
    }

    /**
     * 商品按照主键删除方法
     *
     * @param id 商品主键编号
     * @return 删除商品
     */
    @ApiOperation(value = "按照主键进行商品删除")
    @DeleteMapping("/deleteById")
    public int deleteSale(
            @RequestParam(value = "id") @ApiParam(value = "主键", example = "12", required = true) Integer id) {
        return iSaleService.deleteById(id);
    }

    /**
     * 逻辑删除（假删）
     *
     * @param id 商品编号
     * @return
     */
    @ApiOperation(value = "逻辑删除")
    @DeleteMapping("/deleteByIdLogic")
    public int deleteByIdLogic(
            @RequestParam(value = "flag") @ApiParam(value = "判断", required = true, defaultValue = "0") Integer flag,
            @RequestParam(value = "id") @ApiParam(value = "主键", example = "12", required = true) Integer id
    ) {
        return iSaleService.deleteByIdLogic(id, flag);

    }


}
